//
//  JCChatRoomRuleView.swift
//  MT
//
//  Created by ooops on 2020/5/18.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

protocol JCChatRoomRuleViewDelegate: NSObjectProtocol {
    func didClickCloseButton(sender: UIButton)
}

class JCChatRoomRuleView: UIView {

    weak var delegate: JCChatRoomRuleViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
        
        let ruleString = """
                            为了维护觅颜Me的聊天环境,请在广场聊天时遵守以\n下行为规范：\n\n
                            1.礼貌回应,文明用语,不辱骂、攻击、指责任何觅颜Me用户。\n
                            2.不发布任何广告内容。\n
                            3.不发表煽动国家、地区、民族、宗教间仇恨、歧视、误解等相关言论。\n
                            4.不散布谣言、封建迷信等不实信息。\n
                            5.不散布传播淫秽、色情、暴力、赌博等内容。\n
                            6.不发送钓鱼网站、含有病毒的链接等信息,诱使用户上当受骗蒙受损失。\n\n
                            上述行为一经举报核实后,觅颜Me将会对您的账号进行封号处理。谢谢配合。
                         """
        let attributeString = NSAttributedString(string: "觅颜Me的亲们：\n\n", attributes: [NSAttributedString.Key.font : PingFangSCSemiboldFont(14), NSAttributedString.Key.foregroundColor : UIColor.text.x1d1a1a])
        let mutString = NSMutableAttributedString(string: ruleString, attributes: [NSAttributedString.Key.font : PingFangSCMediumFont(12), NSAttributedString.Key.foregroundColor : UIColor.text.x1d1a1a])
        
        mutString.insert(attributeString, at: 0)
        
        ruleLabel.attributedText = mutString
    }
    
    @objc private func didClickCloseButton(sender: UIButton) {
        self.isHidden = true
    }
    
    lazy var bgView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0x000000, alpha: 0.5)
        return v
    }()
    
    lazy var whiteBgView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 8
        return v
    }()
    
    lazy var titleLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(16)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "广场规范"
        return l
    }()
    
    lazy var closeButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "close-black"), for: .normal)
        btn.addTarget(self, action: #selector(JCChatRoomRuleView.didClickCloseButton(sender:)), for: .touchUpInside)
        return btn
    }()
    
    lazy var scrollView: UIScrollView = {
        let scr = UIScrollView()
        scr.frame = CGRect(x: 0, y: NavigationBarHeight + StatusBarHeight, width: ScreenWidth, height: ScreenHeight - NavigationBarHeight - StatusBarHeight)
        scr.contentSize = CGSize(width: ScreenWidth, height: ScreenHeight - NavigationBarHeight - StatusBarHeight)
        scr.backgroundColor = .white
        scr.contentInsetAdjustmentBehavior = .never
        return scr
    }()
    
    lazy var ruleLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(14)
        l.textColor = UIColor.text.x1d1a1a
//        l.textVerticalAlignment = .top
        l.preferredMaxLayoutWidth = ScreenWidth - 104
        l.numberOfLines = 0
        return l
    }()
    
    lazy var cofrimButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("确定", for: .normal)
        btn.addTarget(self, action: #selector(JCChatRoomRuleView.didClickCloseButton(sender:)), for: .touchUpInside)
        btn.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
        btn.titleLabel?.font = PingFangSCSemiboldFont(16)
        btn.backgroundColor = UIColor(hex: 0xffc000)
        btn.layer.cornerRadius = 20
        btn.layer.masksToBounds = true
        return btn
    }()
    
    private func setupUI() {
        addSubview(bgView)
        addSubview(whiteBgView)
        whiteBgView.addSubview(titleLabel)
        whiteBgView.addSubview(closeButton)
        whiteBgView.addSubview(scrollView)
        scrollView.addSubview(ruleLabel)
        
        whiteBgView.addSubview(cofrimButton)
    }
    
    private func makeConstraint() {
        bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        whiteBgView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.left.equalToSuperview().offset(25)
            make.right.equalToSuperview().offset(-25)
            make.height.equalTo(491 * 375 / 325)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(21)
        }
        
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(21)
            make.left.equalToSuperview().offset(24)
            make.right.equalToSuperview().offset(-24)
            make.bottom.equalTo(cofrimButton.snp.top).offset(-56)
        }
        
        ruleLabel.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        cofrimButton.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-31)
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(40)
        }
        
        closeButton.snp.makeConstraints { (make) in
            make.top.right.equalToSuperview()
            make.size.equalTo(CGSize(width: 36, height: 34))
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }


}
